<template>
  <div class="attendance">
    <h1>考勤管理</h1>
    <el-tabs type="border-card">
      <el-tab-pane>
        <span slot="label"><i class="el-icon-date"></i>考勤记录</span>
        <div class="attendance-search">
      <el-input
        v-model="queryInfo.name"
        placeholder="员工姓名"
        clearable
      ></el-input>
      <el-date-picker
      v-model="queryInfo.date_time"

      placeholder="选择月">
    </el-date-picker>
    <el-button type="primary" @click="search">查询</el-button>
      
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="selection" width="30"> </el-table-column>
      <el-table-column
        type="index"
        :index="1 + (queryInfo.page - 1) * queryInfo.size"
        label="编号"
        width="50"
      >
      </el-table-column>
      <el-table-column prop="nickname" label="姓名" width="50"></el-table-column>
      <el-table-column prop="duty_time" label="工资发放时间" width="120"> </el-table-column>
      <el-table-column prop="clock_in_address" label="上班地址">
        <template slot-scope="scope">
            {{ scope.row.clock_in_address? scope.row.clock_in_address:"暂无" }}
          </template>   
      </el-table-column>
      <el-table-column prop="clock_out_address" label="下班地址">
        <template slot-scope="scope">
            {{ scope.row.clock_out_address? scope.row.clock_out_address:"暂无" }}
          </template>   
      </el-table-column>
      <el-table-column prop="clock_in_time" label="上班时间" width="100">
        <template slot-scope="scope">
            {{ scope.row.clock_in_time? conversionTime(scope.row.clock_in_time):"未打卡" }}
          </template>
         </el-table-column>

      <el-table-column prop="clock_out_time" label="下班时间" width="100">
        <template slot-scope="scope">
            {{ scope.row.clock_out_time ?conversionTime(scope.row.clock_out_time) : '未打卡'}}
          </template>
      </el-table-column>
      <el-table-column prop="clock_in_remark" label="上班备注" width="120">
        <template slot-scope="scope">
            {{ scope.row.clock_in_remark? scope.row.clock_in_remark:"暂无" }}
          </template>   
         </el-table-column>
      <el-table-column prop="clock_out_remark" label="下班备注" width="120"> 
        <template slot-scope="scope">
            {{ scope.row.clock_out_remark? scope.row.clock_out_remark:"暂无" }}
          </template>   
      </el-table-column>
      <!-- <el-table-column prop="clock_status" label="打卡状态">
        <template slot-scope="scope">
            {{ clockTypeList[scope.row.clock_status] }}
          </template>
         </el-table-column> -->
      <el-table-column prop="attendance_status" label="考勤状态" width="80">
        <template slot-scope="scope">
            {{ attendanceTypeList[scope.row.attendance_status] }}
          </template>
 
         </el-table-column>
    </el-table>

     <!-- 分页组件 -->
     <div class="pagination">
        <el-pagination
        @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="queryInfo.size"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      </el-tab-pane>
      <el-tab-pane label="考勤规则">
        <el-descriptions direction="vertical" :column="4" border>
          <el-descriptions-item label="上班时间" :span="12">{{ clockRule.clock_in_time }}</el-descriptions-item>
          <el-descriptions-item label="下班时间" :span="12">{{ clockRule.clock_out_time }}</el-descriptions-item>
          
        </el-descriptions>
        <a-button type="primary" @click="updateDialogVisible = true">
          修改规则
        </a-button>
      </el-tab-pane>
      <el-dialog title="修改考勤规则" :visible.sync="updateDialogVisible" center>
        <el-form :model="clockRuleForm">
        <el-form-item label="上班时间" :label-width="formLabelWidth">
          <el-input v-model="clockRuleForm.clock_in_time" autocomplete></el-input>
        </el-form-item>
        <el-form-item label="下班时间" :label-width="formLabelWidth">
          <el-input v-model="clockRuleForm.clock_out_time" autocomplete></el-input>
        </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRule()">确 定</el-button>
      </div>
      </el-dialog>
    </el-tabs>
    
  </div>
</template>

<script>
import { getHistoryClock,editClockRule,clockRule,getClockType,getAttendanceTypes } from "@/api/clock";
import { getClockList } from "@/api/user";
import { message } from "ant-design-vue";
import dayjs from "dayjs";
export default {
  data() {
    return {
      tableData: [],
      queryInfo: {
        page: 1, //当前页
        size: 10, //每页最大数
      },
      total: 10, //总记录数
      clockRule:{

      },
      clockRuleForm:{},
      updateDialogVisible:false,
      formLabelWidth: '120px',
      clockTypeList:{},
      attendanceTypeList:{}
    };
  },
  created() {
   this.getList();
   this.getClockRule();
   getClockType().then((res)=>{
      this.clockTypeList = res;
    }),
    getAttendanceTypes().then(res=>{
      this.attendanceTypeList=res;
    })
  },
  methods: {
    getList(){
      // getHistoryClock().then(res=>{
      //   this.total = res.total;
      //   this.tableData = res.data;
      // })
      let params = {
        ...this.queryInfo,
        
      }
      if(this.queryInfo.date_time){
        params = {
        ...this.queryInfo,
        date_time: dayjs(this.queryInfo.date_time).format('YYYY-MM-DD')
      }
      }
      getClockList(params).then((res)=>{
        this.total = res.total;
        this.tableData = res.data;
      })
    },
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      this.getList();
    },
    search(){
      this.getList();
    },
    getClockRule(){
      clockRule().then((res)=>{
        this.clockRule = res;
        this.clockRuleForm = res;
      })
    },
    editRule(){
      editClockRule(this.clockRuleForm).then((res)=>{
        this.getClockRule();
        message.success('修改成功!')
        this.updateDialogVisible=false
      })
    },
    conversionTime(time){
      return dayjs(time).format('HH:mm:ss')
    }
  }
};
</script>

<style lang="less" scoped>
.attendance {
  margin: 0 10px;
}
h1 {
  font-size: 24px;
  font-weight: 400;
  padding-bottom: 10px;
  color: #565758;
}
.attendance-search {
  .el-input {
    width: 150px;
    margin-right: 20px;
    margin-bottom: 20px;
  }
}

.pagination {
    width: 400px;
    margin: 10px auto;
  }
</style>
